package org.mcb.zebra.portal.resource;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletContext;

import org.apache.commons.digester.Digester;
import org.mcb.zebra.portal.page.ZebPage;
import org.xml.sax.SAXException;

public class ZebResourceConfigReader extends ResourceConfigReader {
	private final String resourcePath = "/WEB-INF/mcb-portal-config.xml";
	private Digester digester = new Digester();
	/* (non-Javadoc)
	 * @see org.mcb.zebra.portal.resource.ResourceConfigReader#init()
	 */
	public void init() {
		digester.setClassLoader(Thread.currentThread().getContextClassLoader());
		digester.addObjectCreate("mcb-portal-config", ZebResourceConfig.class);
		digester.addBeanPropertySetter("mcb-portal-config/mcb-portal-info","portalInfo");
		digester
				.addCallMethod("mcb-portal-config/mcb-supported-portlet-modes/portlet-mode","addSupportedPortletMode",0);
		digester
				.addCallMethod("mcb-portal-config/mcb-supported-window-states/window-state","addSupportedWindowState",0);
		digester.addObjectCreate("mcb-portal-config/mcb-page-manager/page", ZebPage.class);
		digester.addSetProperties("mcb-portal-config/mcb-page-manager/page");
		digester.addCallMethod("mcb-portal-config/mcb-page-manager/page/portlet", "addPortletWindow",2);
		digester.addCallParam("mcb-portal-config/mcb-page-manager/page/portlet", 0,"contextPath");
		digester.addCallParam("mcb-portal-config/mcb-page-manager/page/portlet", 1,"portletName");
		digester.addSetNext("mcb-portal-config/mcb-page-manager/page","addPage");
		digester.addCallMethod("mcb-portal-config/mcb-page-manager/default-page","setDefaultPage",0);
		
	}

	/* (non-Javadoc)
	 * @see org.mcb.zebra.portal.resource.ResourceConfigReader#getConfig(javax.servlet.ServletContext, java.lang.String)
	 */
	public ResourceConfig getConfig(ServletContext context, String resource)
			throws IOException, SAXException {
		InputStream inputStream = context.getResourceAsStream(resource);
		if (inputStream != null)
			return (ResourceConfig)digester.parse(inputStream);
		return null;
	}

	/* (non-Javadoc)
	 * @see org.mcb.zebra.portal.resource.ResourceConfigReader#getConfig(javax.servlet.ServletContext)
	 */
	public ResourceConfig getConfig(ServletContext context) throws IOException, SAXException {
		return getConfig(context, resourcePath);
	}

	/* (non-Javadoc)
	 * @see org.mcb.zebra.portal.resource.ResourceConfigReader#getConfig(java.lang.String)
	 */
	public ResourceConfig getConfig(String resource) throws IOException, SAXException {
		InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
		if (inputStream != null)
			return (ResourceConfig)digester.parse(inputStream);
		return null;
	}
	/* (non-Javadoc)
	 * @see org.mcb.zebra.portal.resource.ResourceConfigReader#getConfig(java.io.InputStream, java.lang.String)
	 */
	public ResourceConfig getConfig(InputStream inputStream,String resource) throws IOException, SAXException{
		if (inputStream != null)
			return (ResourceConfig)digester.parse(inputStream);
		return null;
	}
}
